package net;

import game.Livello;
import grafica.NetPanel;

import java.awt.Image;
import java.awt.MediaTracker;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JPanel;

import sprite.Giocatore;

public class Server {
    
    ServerSocket serverSocket = null;
    boolean listening = true;
    static ArrayList<PrintWriter> clientList = new ArrayList<PrintWriter>();
    static ArrayList<String> user = new ArrayList<String>();
    NetPanel net;
    static Livello stato;
     
    public Server() {
        stato = new Livello("netlevel/due.rgl",2,false);

        try {
            serverSocket = new ServerSocket(4444);
            System.out.println("Il server del gioco e' in ascolto");
                       
            
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }
        
        while (listening)  {
            try {
                Socket socket = serverSocket.accept();
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                ObjectOutputStream obj = new ObjectOutputStream(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));               
                clientList.add(out);
                new ServerMulti(out,obj,in,socket);
            	for(PrintWriter o:Server.clientList) {
            		System.out.println(Server.clientList.indexOf(o));
            	}  	                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
                     

        }
        
            try {              
                serverSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

    }
    

    /*
    public static void main(String args[]) {
    	Server s = new Server();
    }
    
    */

    
}